<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
// use \app\index\model\Register as RegModel;


// 注册
class Register extends Controller
{

    // 跳转注册页面
    public function index()
    {   

        $category = DB::table('category')->where('pid','0,')->select();

        
        return view('index@register/register',['category' => $category]);
    }



    // 提交数据
    public function doRegister(Request $request)
    {
        return json('qqqqq');
        // var_dump($tel,$pass);
        // 接收数据
        $data = $request->post();
        var_dump($data);die;

        $telcheck = '/^[1]{1}[0-9]{10}/';

    // 判断有误
        if ($data['tel'] == null) {
                                   
            return $this->error('电话号码为空,请重新输入...');

        } else if (!preg_match ( $telcheck,$data['tel'])) {

            return $this->error('请输入有效的电话号码');

        }


        if ($data['pass'] != $data['repass']) {

            return $this->error('两次密码不一致,请重试...');

        }


       // 查询
        $find = Db::table('hmy_user')->field(['userpass'],true)->select();

        foreach ($find as $k => $v) {

            if ($data['tel'] == $v['tel']) {

               return $this->error('电话号码已存在,可直接登录...');

            }            

            if ($data['name'] == $v['name']) {

               return $this->error('用户名已存在,请重新输入..');
               
            }

            // var_dump($v);
        }
        // var_dump($find);


        // 实例化medel
        // $model = new RegModel();
        // // $model->data($data);
        
        // // 调用model中的方法加密
        // $model->pass = $model->checkpass($data['pass']);
        
        $data['pass'] = md5($data['pass']);
        unset($data['repass']);
        // var_dump($data);



        // 执行sql
        $result = Db::table('hmy_user')->data($data)->insert();

        // 返回结果
        if ($result > 0) {

             return $this->success('注册成功,请登录..', url('index/Login/index'));

        } else {

             return $this->error('注册失败,请重试...');

        }
        

    }
}
